package com.voicechat.firstmet.activity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.voicechat.firstmet.R;
import com.voicechat.firstmet.adapter.ViewPagerAdapter;
import com.voicechat.firstmet.base.BaseActivity;
import com.voicechat.firstmet.bean.MySelfInfoBean;
import com.voicechat.firstmet.bean.ShareGirlsDetailsBean;
import com.voicechat.firstmet.client.OssConfig;
import com.voicechat.firstmet.net.HTTPAPI;
import com.voicechat.firstmet.net.service.FaintService;
import com.voicechat.firstmet.utils.GlideLoadUtils;
import com.voicechat.firstmet.utils.ParcelableUtils;
import com.voicechat.firstmet.utils.PhoneUtils;
import com.voicechat.firstmet.utils.SPUtils;
import com.voicechat.firstmet.utils.StatusBarUtils;
import com.voicechat.firstmet.utils.Utils;
import com.voicechat.firstmet.view.CircleImageView;
import com.voicechat.firstmet.view.LabelsView;

import java.util.ArrayList;
import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @ProjectName: FirstMet
 * @Package: com.voicechat.firstmet.activity
 * @ClassName: ShareGirlsDetailsActivity
 * @Description: 共享闺蜜详情
 * @Author: ljp
 * @CreateDate: 2019/1/17 11:39
 * @Version: 1.0
 */
public class ShareGirlsDetailsActivity extends BaseActivity {
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    @BindView(R.id.tv_girls_nikname)
    TextView tvGirlsNikname;
    @BindView(R.id.image_sex)
    ImageView imageSex;
    @BindView(R.id.tv_age)
    TextView tvAge;
    @BindView(R.id.linear_sex_age)
    LinearLayout linearSexAge;
    @BindView(R.id.largeLabe2)
    LabelsView largeLabe2;
    @BindView(R.id.circle_icon_promulgator)
    CircleImageView circleIconPromulgator;
    @BindView(R.id.tv_nike_name_promulgator)
    TextView tvNikeNamePromulgator;
    @BindView(R.id.image_sex_promulgator)
    ImageView imageSexPromulgator;
    @BindView(R.id.tv_age_promulgator)
    TextView tvAgePromulgator;
    @BindView(R.id.linear_back_age)
    LinearLayout linearBackAge;
    @BindView(R.id.image_vip)
    ImageView imageVip;
    @BindView(R.id.image_is_real)
    ImageView imageIsReal;
    @BindView(R.id.tv_share_girls_info)
    TextView tvShareGirlsInfo;
    @BindView(R.id.image_back)
    ImageView imageBack;
    @BindView(R.id.image_more_tr)
    ImageView imageMoreTr;
    @BindView(R.id.linear_transparency)
    LinearLayout linearTransparency;
    @BindView(R.id.tv_open_vip)
    TextView tvOpenVip;
    @BindView(R.id.rela_vip)
    RelativeLayout relaVip;
    @BindView(R.id.tv_share_red)
    TextView tvShareRed;
    @BindView(R.id.tv_vip_parse)
    TextView tvVipParse;
    @BindView(R.id.tv_pay)
    TextView tvPay;
    @BindView(R.id.tv_images)
    TextView tvImages;
    @BindView(R.id.linear_2)
    LinearLayout linear2;
    private MySelfInfoBean.ObjectBean mySeleInfo;
    private String token;
    private String deviceInfo;
    private String id;
    private String TAG = "ShareGirlsDetailsActivity";
    private ArrayList<ImageView> imageViews;
    private static String[] imgs;
    private ShareGirlsDetailsBean.MapBean.MdFriendBean mdFriend;
    private ShareGirlsDetailsBean.MapBean.MdUserBean mdUser;
    private VpAdapter vpAdapter;
    private ArrayList<String> strings;
    private String price;

    @Override
    protected int setStatusBarColor() {
        return 0;
    }

    @Override
    protected boolean isSettingStatusBarText() {
        return false;
    }

    @Override
    protected int loadLayout() {
        return R.layout.activity_share_girls_details;
    }

    @Override
    protected void initLayout(Bundle savedInstanceState) {
        ButterKnife.bind(this);
        //透明状态栏
        StatusBarUtils.Full(this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            this.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
        mySeleInfo = MySelfInfoBean.ObjectBean.CREATOR.createFromParcel(ParcelableUtils.unmarshall(
                SPUtils.getEditoString(SPUtils.SP_MYSELFINFO)));
        token = SPUtils.getEditoString(SPUtils.SP_TOKEN);
        deviceInfo = PhoneUtils.getDeviceInfo(context);
        id = getIntent().getStringExtra("id");//闺蜜信息id
        price = getIntent().getStringExtra("Price");
        strings = new ArrayList<>();
        Log.i(TAG, "initLayout: ---------" + id);
        initHttpData();
    }
    /**
     * @return
     * @method
     * @description 网络请求
     * @date: 2019/1/18 13:53
     * @author: ljp
     */
    private void initHttpData() {
        FaintService.shareGirlsDetails(token, deviceInfo, id).subscribe(shareGirlsBean -> {
            Log.i(TAG, "initHttpData: ---------" + shareGirlsBean.toString());
            if (shareGirlsBean.getCode() == HTTPAPI.RETURN_CODE_TYPE.RETRUN_SUCCESS) {
                initView(shareGirlsBean);
            } else if (shareGirlsBean.getCode() == HTTPAPI.RETURN_CODE_TYPE.RETURN_NO_TOKEN) {
                Utils.setToken(context);
            } else {
                toast_MakeText(shareGirlsBean.getMsg());
            }
        }, throwable -> {
            toast_MakeText(getResources().getString(R.string.net_error));
        });
    }

    /**
     * @return
     * @method
     * @description 数据空间实例化
     * @date: 2019/1/18 13:59
     * @author: ljp
     */
    private void initView(ShareGirlsDetailsBean shareGirlsBean) {
        mdFriend = shareGirlsBean.getMap().getMdFriend();
        mdUser = shareGirlsBean.getMap().getMdUser();
        if (mdUser.getId().equals(mySeleInfo.getId())) {
            linear2.setVisibility(View.GONE);
        }else {
            linear2.setVisibility(View.VISIBLE);
            if (mySeleInfo.getvType().equals("0")) {// 会员类型  0非会员 1月会员 2年会员
                relaVip.setVisibility(View.VISIBLE);
            } else {
                relaVip.setVisibility(View.GONE);
            }
        }
        imgs = Utils.spitPictureUrl(mdFriend.getAvatarUrl());
        initImages();
        vpAdapter = new VpAdapter();
        viewPager.setAdapter(vpAdapter);
        tvImages.setText("1/" + imgs.length);
        //闺蜜信息
        strings.add(mdFriend.getAge() + "岁");
        strings.add(mdFriend.getHeight() + "cm  " + mdFriend.getWeight() + "kg");
        strings.add(mdFriend.getEducation());
        tvGirlsNikname.setText(mdFriend.getNickName());
        largeLabe2.setLabels(strings);
        tvAge.setText(mdFriend.getAge() + "岁");
        if (mdFriend.getGender() == 1) {//判断性别  1男 2女
            imageSex.setImageResource(R.mipmap.boy);
            linearSexAge.setBackgroundResource(R.mipmap.sex_blue);
        } else {
            imageSex.setImageResource(R.mipmap.girl);
            linearSexAge.setBackgroundResource(R.mipmap.sex_red);
        }
        tvShareGirlsInfo.setText(mdFriend.getInfo());
        tvShareRed.setText("￥" + mdFriend.getPrice());
        int i = (int) Math.ceil(mdFriend.getPrice() * 0.8);
        tvVipParse.setText("会员价：￥" +(i));
       // Log.i(TAG, "initView: ---getPrice----"+i);
        //发布者信息
        GlideLoadUtils.getInstance().glideLoad(context, OssConfig.OSS_HTTP + mdUser.getAvatarUrl(),
                circleIconPromulgator);
        tvNikeNamePromulgator.setText(mdUser.getNickName());
        if (mdUser.getVType() == 1) {//判断是vip等级 0非会员 1月会员 2年会员
            imageVip.setVisibility(View.VISIBLE);
            imageVip.setImageResource(R.mipmap.vip_month);
        } else if (mdUser.getVType() == 2) {
            imageVip.setVisibility(View.VISIBLE);
            imageVip.setImageResource(R.mipmap.vip_year);
        } else {
            imageVip.setVisibility(View.GONE);
        }
        if (mdUser.getGender() == 1) {//判断性别  1男 2女
            imageSexPromulgator.setImageResource(R.mipmap.boy);
            linearBackAge.setBackgroundResource(R.mipmap.sex_blue);
        } else if (mdUser.getGender() == 2) {
            imageSexPromulgator.setImageResource(R.mipmap.girl);
            linearBackAge.setBackgroundResource(R.mipmap.sex_red);
        }
        if (!TextUtils.isEmpty(mdUser.getBirthday())) {
            String substring = mdUser.getBirthday().substring(0, 4);
            String time = Utils.getStringTime(new Date());
            int age = (Integer.parseInt(time)) - (Integer.parseInt(substring));
            tvAgePromulgator.setText(age + "");
        }
        //实名认证任务 0未认证 1 已审核 2 审核中 3驳回
        if (mdUser.getIsAttestation() == 1) {
            imageIsReal.setVisibility(View.VISIBLE);
        } else {
            imageIsReal.setVisibility(View.GONE);
        }
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {
            }

            @Override
            public void onPageSelected(int i) {
                tvImages.setText(i + 1 + "/" + imgs.length);
            }

            @Override
            public void onPageScrollStateChanged(int i) {
            }
        });


    }

    @OnClick({R.id.image_back, R.id.image_more_tr, R.id.tv_open_vip, R.id.rela_vip, R.id.tv_pay,R.id.linear_other_share_girls})
    public void onClick(View view) {
        Intent intent  = null;
        switch (view.getId()) {
            case R.id.image_back:
                finish();
                break;
            case R.id.image_more_tr:
                break;
            case R.id.tv_open_vip:
                intent = new Intent(activity, BuyVIPActivity.class);
                intent.putExtra("data", "");
                startActivity(intent);
                break;
            case R.id.rela_vip:
                break;
            case R.id.tv_pay:
                intent = new Intent(activity, BlindDateDemandActivity.class);
                intent.putExtra("id",id);
                intent.putExtra("Price",price);
                startActivity(intent);
                break;
            case R.id.linear_other_share_girls:
                intent = new Intent(activity, OtherShareGirlsActivity.class);
                intent.putExtra("mdUser",mdUser);
                startActivity(intent);
                break;
        }
    }

    /**
     * @return
     * @method
     * @description 图片数据
     * @date: 2019/1/18 13:58
     * @author: ljp
     */
    private void initImages() {
        //设置每一张图片都填充窗口
        ViewPager.LayoutParams mParams = new ViewPager.LayoutParams();
        imageViews = new ArrayList<ImageView>();
        for (int i = 0; i < imgs.length; i++) {
            ImageView iv = new ImageView(this);
            iv.setLayoutParams(mParams);//设置布局
            GlideLoadUtils.getInstance().glideLoad(context, OssConfig.OSS_HTTP + imgs[i],
                    iv);
            iv.setScaleType(ImageView.ScaleType.CENTER_CROP);//这里也是一个图片的适配
            imageViews.add(iv);
        }
    }

    public class VpAdapter extends ViewPagerAdapter {
        @Override
        public int getCount() {
            return imageViews.size();
        }

        @Override
        public int getItemPosition(Object object) {
            return POSITION_NONE;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(imageViews.get(position));
            return imageViews.get(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }
}
